package com.xlh.dao.conclude.helper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xlh.dao.conclude.ConcludeScoreMapper;
import com.xlh.pojo.conclude.ConcludeScore;
import com.xlh.pojo.conclude.example.ConcludeScoreExample;
import com.xlh.util.SpringContextUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * @author cfz
 */
@Repository
public class ConcludeScoreMapperHelper {

    @Autowired
    private ConcludeScoreMapper concludeScoreMapper;


    public void deleteBySubId(List<Long> subIds) {

        ConcludeScore record = new ConcludeScore();
        record.setIsDelete(true);
        ConcludeScoreExample example = new ConcludeScoreExample();
        example.createCriteria().andCheckSubIdIn(subIds);
        concludeScoreMapper.updateByExampleSelective(record, example);
    }


    /**
     * 根基 考核项ID 和 考核分项ID 和学生编号 关联成绩
     * @param checkId
     * @param checkSubId
     * @param sno
     * @return
     */
    public ConcludeScore getCheckSubScore(Long checkId, Long checkSubId, String sno) {

        ConcludeScoreExample example = new ConcludeScoreExample();
        example.createCriteria().andIsDeleteEqualTo(false).andCheckIdEqualTo(checkId).andCheckSubIdEqualTo(checkSubId).andStudentNoEqualTo(sno);
        List<ConcludeScore> concludeScores = concludeScoreMapper.selectByExample(example);

        if(CollectionUtil.isNotEmpty(concludeScores)){
            return concludeScores.get(0);
        }
        return null;
    }

    /**
     * 根据SUB_ID查询当前学生信息
     * @param checkSubId
     * @return
     */
    public List<ConcludeScore> findCheckSubScoreBySubId(Long checkSubId, List<String> studentNos) {

        ConcludeScoreExample example = new ConcludeScoreExample();
        example.createCriteria().andCheckSubIdEqualTo(checkSubId).andStudentNoIn(studentNos).andIsDeleteEqualTo(false);
        List<ConcludeScore> concludeScores = concludeScoreMapper.selectByExample(example);

        return concludeScores;
    }


    /**
     * 查询根据subId 和 学生no 查询
     * 逻辑上此信息唯一
     * @param checkSubId
     * @param sno
     * @return
     */
    public ConcludeScore findCheckSubScoreBySubIdAndSno(Long checkSubId, String sno) {

        ConcludeScoreExample example = new ConcludeScoreExample();
        example.createCriteria().andCheckSubIdEqualTo(checkSubId).andStudentNoEqualTo(sno).andIsDeleteEqualTo(false);
        List<ConcludeScore> concludeScores = concludeScoreMapper.selectByExample(example);

        if(CollectionUtil.isNotEmpty(concludeScores)){

            return concludeScores.get(0);
        }
        return null;
    }


    /**
     * 根据主键值进行更新操作
     */
    public void updateBatch( List<ConcludeScore> concludeScores ){

        SqlSession session = SpringContextUtil.getBean(SqlSessionFactory.class).openSession(ExecutorType.BATCH);
        try {
            ConcludeScoreMapper mapper  = session.getMapper(ConcludeScoreMapper.class);
            for (ConcludeScore concludeScore : concludeScores) {
                mapper.updateByPrimaryKeySelective(concludeScore);
            }
        } catch (Exception e) {
            e.printStackTrace();
            session.close();
        } finally {
            session.commit();
            session.close();
        }

    }

    /**
     * 执行插入操作
     */
    public void insert(ConcludeScore concludeScore) {

        concludeScoreMapper.insertSelective(concludeScore);
    }

    /**
     * 主键值更新
     * @param concludeScore
     */
    public void update(ConcludeScore concludeScore) {

        concludeScoreMapper.updateByPrimaryKeySelective(concludeScore);
    }

    public void deleteBySubIdAndSno(List<ConcludeScore> insertRecords) {



        for (ConcludeScore insertRecord : insertRecords) {

            String studentNo = insertRecord.getStudentNo();
            Long checkSubId = insertRecord.getCheckSubId();
            ConcludeScore concludeScore = new ConcludeScore();
            concludeScore.setIsDelete(true);
            ConcludeScoreExample condition = new ConcludeScoreExample();
            condition.createCriteria().andStudentNoEqualTo(studentNo).andCheckSubIdEqualTo(checkSubId);
            concludeScoreMapper.updateByExampleSelective(concludeScore, condition);
        }
    }
}
